require(['jquery', 'cookie', 'app', 'lay','wangEditor'], function ($) {
    require(['vue'], function (Vue) {
        var base = new Vue({
            el: "#base",
            data:{
                list: "",
                listCol:["优惠标题","优惠内容","次序"],
                config:"",
                th:{
                    uuid:"",
                    bt:"",
                    val:"",
                    cx:""
                }
            },
            created: function () {
                this.loads();
                this.loadPage(0,100);
                this.getConfig();
            },
            methods: {
                editDBL: function (e) {
                    var e = e.target;
                    $(e).html("<input class='layui-input' onblur='editBlur(this)' value='"+$(e).text().trim()+"'/>");
                },
                editDBLTEXT: function (e) {
                    var e = e.target;
                    $(e).html("<textarea class='layui-textarea' onblur='editBlur(this)'>"+$(e).text().trim()+"</textarea>");
                },
                editDBLIMG: function (e) {
                    var e = e.target;
                    $("#logo").click();
                },
                loadPage:function (begin,end) {
                    var jon={};
                    var sql="select uuid,bt,val,cx from small_shaky where 1=1";
                    sql+=" order by uuid limit "+begin+","+end;
                    jon['sql']=encodeURI(encodeURI(sql));
                    this.list=JSON.parse(playAjax("101",JSON.stringify(jon)))['data'];
                },
                referXadmin: function () {
                    window.location.reload();
                },
                getConfig:function () {
                    this.config=JSON.parse(playAjax("1555508458600",""))['data'];
                },
                loads:function () {
                    layui.use(['upload','element','layer'], function(){
                        var upload = layui.upload;
                        //执行实例
                        var element = layui.element;
                        var layer = layui.layer;
                        var uploadInst = upload.render({
                            elem: '#logo' //绑定元素
                            ,url: base.config['server_img_interface']+'/ImgController/upload?project='+base.config['appID']//上传接口
                            ,done: function(res){
                                //上传完毕回调
                                alr(res['msg']);
                                base.config.appImg=base.config['server_img_view']+"/"+res['data']['src'];
                                element.progress('demo', '0%');
                                layer.closeAll('loading');
                            }
                            ,error: function(){
                                //请求异常回调
                                alr("上传失败！请联系管理员。")
                                element.progress('demo', '0%');
                                layer.closeAll('loading');
                            }
                            ,progress:function(value){//上传进度回调 value进度值
                                element.progress('demo', value+'%')//设置页面进度条
                            }
                            ,accept: 'file' //普通文件
                            ,xhr:xhrOnProgress
                            ,before: function(obj){
                                layer.load(); //上传loading
                            }
                        });
                    });
                },
                checkSure: function () {
                    if ($(".zh").length <= 0) {
                        alr("未选中！请选择一条记录");
                        return false;
                    } else {
                        var td = $(".zh").find("td");
                        var uuid = $(td[1]).text().trim();
                        return uuid;
                    }
                },
                deleted:function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    layer.open({
                        content: '确定要删除吗？'
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            var jon = {};
                            jon['uuid'] = base.checkSure();
                            var re = JSON.parse(playAjax("1555508458602", JSON.stringify(jon)));
                            alr(re['title']);
                            //按钮【按钮一】的回调
                            layer.closeAll();
                            base.referXadmin();
                        }
                        , btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            layer.closeAll();
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                        , cancel: function () {
                            //右上角关闭回调
                            layer.closeAll();
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                },
                show_add:function () {
                    $("#list").hide();
                    $("#add").show();
                },
                show_update:function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    var td = $(".zh").find("td");
                    var xv = ($(td[0]).text()) - 1;
                    this.th = this.list[xv];
                    $("#list").hide();
                    $("#update").show();
                    setTimeout(function () {
                        referFrom();
                    }, 0)
                },
                pre_home: function () {
                    $("#list").show();
                    $("#add").hide();
                },
                up_save:function () {
                    var jon=fromName($("#appx"));
                    jon['appImg']=this.config['appImg'];
                    delete jon['undefined'];
                    var re=JSON.parse(playAjax("1555508458601",JSON.stringify(jon)));
                    alr(re['title']);
                    if(re['code']=='0'){
                        setTimeout(function () {
                            base.referXadmin();
                        },500);
                    }
                }
            }
        });
        layui.use('form', function () {
            var form = layui.form;
            form.render();
            form.on('submit(formDemo)',function (data) {
                var jon=data.field;
                var re=JSON.parse(playAjax("1555508458603",JSON.stringify(jon)));
                alr(re['title']);
                if(re['code']=='0'){
                    setTimeout(function () {
                        base.referXadmin();
                    },500);
                }
                return false;
            });
            form.on('submit(formDemos)',function (data) {
                var jon=data.field;
                jon['uuid']=base.th['uuid'];
                var re=JSON.parse(playAjax("1555508458604",JSON.stringify(jon)));
                alr(re['title']);
                if(re['code']=='0'){
                    setTimeout(function () {
                        base.referXadmin();
                    },500);
                }
                return false;
            });
        });

    });
});
var xhrOnProgress=function(fun) {
    xhrOnProgress.onprogress = fun; //绑定监听
    //使用闭包实现监听绑
    return function() {
        //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
        var xhr = $.ajaxSettings.xhr();
        //判断监听函数是否为函数
        if (typeof xhrOnProgress.onprogress !== 'function')
            return xhr;
        //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
        if (xhrOnProgress.onprogress && xhr.upload) {
            xhr.upload.onprogress = xhrOnProgress.onprogress;
        }
        return xhr;
    }
}